<?php


namespace app\common\constant;


class ErrorNum
{
    private static $instance = null;
    /**
     * 0 success
     */
    const SUCCESS = 0;

    /**
     * 1xx 系统类异常码
     */
    const SYS_ERROR = 10001; // 系统未知错误
    const DB_ERROR = 10002; // 系统数据库错误


    /**
     * 2xx 常规类异常码
     */
    const PARAM_ILLEGAL = 20001; // 参数错误
    const TOO_MANY_REQUEST = 20002; // 过多的请求
    const TOKEN_INVALID = 20003; // token invalid
    const UPLOAD_FAIL = 20004; //上传失败
    const VERIFY_CODE_ILLEGAL = 20005; //验证码错误
    const ADD_FAIL = 20006; //添加失败
    const MODIFY_FAIL = 20007; //修改失败
    const DEL_FAIL = 20008; //删除失败

    /**
     * 3xx 业务逻辑异常
     */
    const SEND_SMS_FAIL = 30001; // 短信发送失败
    const NO_SCAN = 30002; // 未扫码
    const NO_REGISTER = 30003;// 未注册
    const NO_LOGIN = 30004;// 未登录
    const NO_USER = 30005; //没有这个用户
    const ERROR_PASSWORD = 30006; //密码错误
    const NO_AUTH = 30007;//没有权限

    private $message = [
        ErrorNum::SUCCESS => "ok",
        ErrorNum::SYS_ERROR => "系统未知错误",
        ErrorNum::DB_ERROR => "系统数据库错误",
        ErrorNum::PARAM_ILLEGAL => "参数错误",
        ErrorNum::TOO_MANY_REQUEST => "过多的请求",
        ErrorNum::TOKEN_INVALID => "token invalid",
        ErrorNum::UPLOAD_FAIL => "上传失败",
        ErrorNum::VERIFY_CODE_ILLEGAL => "验证码错误",
        ErrorNum::ADD_FAIL => "添加失败",
        ErrorNum::MODIFY_FAIL => "修改失败",
        ErrorNum::DEL_FAIL => "删除失败",
        ErrorNum::SEND_SMS_FAIL => "短信发送失败",
        ErrorNum::NO_SCAN => "未扫码",
        ErrorNum::NO_REGISTER => "未注册",
        ErrorNum::NO_LOGIN => "未登录",
        ErrorNum::NO_USER => "没有这个用户",
        ErrorNum::ERROR_PASSWORD => "密码错误",
        ErrorNum::NO_AUTH => "没有权限",
    ];

    public function getInstance(): ErrorNum
    {
        if (is_null(self::$instance)) {
            self::$instance = new self;
        }
        return self::$instance;
    }

    public function getMessage($code): string
    {
        return $this->message[$code] ?? "未知";
    }

}